<template>
  <div>
    <template v-if="ishead && itemdata.lCanOutput === '1'">
      <template v-if="itemdata.lCanOutput === '0'">
        <input v-model="itemdata.value" style="display: none" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :error="isRed == 'red'" clearable size="large" :id="itemdata.sField + '_ref'" required readonly
          placeholder="系统自动填充" v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :error="isRed == 'red'" readonly size="large" v-if="itemdata.sUITypeExp == 'normal'"
          placeholder="请选择" :id="itemdata.sField + '_ref'" required clearable v-model="itemdata.value"
          :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)"
          @change="openRef(itemdata, 'headdata', '', 0)" @click="openRef(itemdata, 'headdata', '', 0)"
          @click-right-icon.stop="openRef(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
        <van-field :error="isRed == 'red'" size="large" v-else-if="itemdata.sUITypeExp == 'multi'" placeholder="请选择"
          :id="itemdata.sField + '_ref'" required clearable v-model="itemdata.value" :label="itemdata.sTitle"
          right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)"
          @click="openRefMulti(itemdata, 'headdata', '', 0)"
          @click-right-icon.stop="openRefMulti(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :error="isRed == 'red'" clearable size="large" :id="itemdata.sField + '_ref'" placeholder="系统自动填充"
          readonly v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :error="isRed == 'red'" readonly size="large" v-if="itemdata.sUITypeExp == 'normal'"
          placeholder="请选择" :id="itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle"
          right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)"
          @change="openRef(itemdata, 'headdata', '', 0)" @click="openRef(itemdata, 'headdata', '', 0)"
          @click-right-icon.stop="openRef(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
        <!-- <van-field
         :error="isRed == 'red'" size="large" v-else-if="itemdata.sUITypeExp == 'multi'" placeholder="请选择" :id="itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click="openRef(itemdata, 'bodydata', indexName, index)" @click-right-icon.stop="openRefMulti(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" /> -->
        <van-field :error="isRed == 'red'" readonly size="large" v-else-if="itemdata.sUITypeExp == 'multi'"
          placeholder="请选择" :id="itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle"
          right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)"
          @click="openRefMulti(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else-if="itemdata.sUITypeExp == 'normal'">
      <template v-if="itemdata.lCanOutput === '0'">
        <input v-model="itemdata.value" style="display: none" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :error="isRed == 'red'" clearable size="large" :id="index + '_' + itemdata.sField + '_ref'" required
          placeholder="系统自动填充" readonly v-model="itemdata.value" :label="itemdata.sTitle"
          :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :error="isRed == 'red'" readonly size="large" placeholder="请选择"
          :id="index + '_' + itemdata.sField + '_ref'" required clearable v-model="itemdata.value"
          :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)"
          @change="openRef(itemdata, 'bodydata', indexName, index)"
          @click="openRef(itemdata, 'bodydata', indexName, index)" @click-right-icon.stop="
            openRef(itemdata, 'bodydata', indexName, index)
            " :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :error="isRed == 'red'" clearable size="large" :id="index + '_' + itemdata.sField + '_ref'"
          placeholder="系统自动填充" readonly v-model="itemdata.value" :label="itemdata.sTitle"
          :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :error="isRed == 'red'" readonly size="large" placeholder="请选择"
          :id="index + '_' + itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle"
          right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)"
          @change="openRef(itemdata, 'bodydata', indexName, index)"
          @click="openRef(itemdata, 'bodydata', indexName, index)" @click-right-icon.stop="
            openRef(itemdata, 'bodydata', indexName, index)
            " :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else-if="itemdata.sUITypeExp == 'multi'">
      <template v-if="itemdata.lCanOutput === '0'">
        <input v-model="itemdata.value" style="display: none" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :error="isRed == 'red'" clearable size="large" :id="index + '_' + itemdata.sField + '_ref'" required
          placeholder="系统自动填充" readonly v-model="itemdata.value" :label="itemdata.sTitle"
          :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :error="isRed == 'red'" readonly clearable size="large" placeholder="请选择"
          :id="index + '_' + itemdata.sField + '_ref'" required v-model="itemdata.value" :label="itemdata.sTitle"
          right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)"
          @click="openRefMulti(itemdata, 'bodydata', indexName, index)" @click-right-icon.stop="
            openRefMulti(itemdata, 'bodydata', indexName, index)
            " :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :error="isRed == 'red'" clearable size="large" :id="index + '_' + itemdata.sField + '_ref'"
          placeholder="系统自动填充" readonly v-model="itemdata.value" :label="itemdata.sTitle"
          :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :error="isRed == 'red'" readonly clearable size="large" placeholder="请选择"
          :id="index + '_' + itemdata.sField + '_ref'" v-model="itemdata.value" :label="itemdata.sTitle"
          right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)"
          @click="openRefMulti(itemdata, 'bodydata', indexName, index)" @click-right-icon.stop="
            openRefMulti(itemdata, 'bodydata', indexName, index)
            " :error-message="itemdata.errormsg" />
      </template>
    </template>
  </div>
</template>
<script>
import { Toast } from "vant";
import Vue from "vue";
import { base64encode } from "@/assets/js/tool.js";
import { eventMixins } from "@/mixins/event.js";
Vue.use(Toast);
export default {
  mixins: [eventMixins],
  data: function () {
    return {
      isRed: '',
      // clickmodel: {},
      // clickfrom: '',
      // clickindexName: '',
      // clickindex: 0
    };
  },
  // props: ['clickmodel', 'clickfrom', 'clickindexName', 'clickindex', 'jsondata', 'indexName', 'itemdata', 'ishead', 'index', 'activeNames'],
  props: {
    clickmodel: {
      type: Object,
      required: false,
    },
    clickfrom: {
      type: String,
      required: false,
    },
    clickindexName: {
      type: String,
      required: false,
    },
    clickindex: {
      type: Number,
      required: false,
    },
    jsondata: {
      type: Object,
      required: false,
    },
    indexName: {
      type: String,
      required: false,
    },
    itemdata: {
      type: Object,
      required: false,
    },
    fromcardid: {
      type: String,
      required: false,
    },
    ishead: {
      type: Boolean,
      default: false,
    },
    index: {
      type: Number,
      required: false,
    },
    jsondataindex: {
      type: Number,
      required: false,
    },
    jsondatai: {
      type: Number,
      required: false,
    },
    updateCustomerTagArr: {
      type: Function,
      default: () => { },
    },
  },
  mounted() {
    this.isRed = this.itemdata?.sFormat.split(';').filter(i => {
      return i == 'fontcolor:red'
    })[0]?.split(':')[1]
  },
  watch: {
    "itemdata.value": {
      async handler(newval) {
        this.$emit("handleFiledLink", {
          sField: this.itemdata.sField,
          value: newval,
          ishead: this.ishead,
          index: this.index,
        });
        // 事件函数
        if (this.itemdata?.event && this.itemdata?.event?.length > 0) {
          const eventArr = this.itemdata.event;
          for (let i = 0; i < eventArr.length; i++) {
            const eventItem = eventArr[i];

            // 事件函数
            if (this.itemdata?.event && this.itemdata?.event?.length > 0) {
              const eventArr = this.itemdata.event;
              for (let i = 0; i < eventArr.length; i++) {
                const eventItem = eventArr[i];

                if (eventItem.eventSource === "tagDisplay(this);") {
                  const res = await this.tagDisplay(this.itemdata);
                  const customerTagArr = this.handleTagDisplayRes(res);
                  this.updateCustomerTagArr(customerTagArr);
                }

                if (eventItem.eventSource === "fillTitle();") {
                  this.fillTitle(this.jsondata, this.itemdata, newval);
                }

                if (eventItem.eventSource === "fillTitle2();") {
                  this.fillTitle2(this.jsondata, this.itemdata, newval);
                }
              }
            }
          }
        }
      },
      immediate: true,
    },
  },
  methods: {
    clearRefValue(datamodel) {
      // if (!datamodel.value) {
      //   return; // 如果输入框的值为空，不做任何操作
      // }
      // datamodel.value = ''; // 清空输入框的值
      if (datamodel.value === "") {
        datamodel.valuehidden = "";
      }
    },
    openRef(datamodel, datafrom, indexName, index) {
      console.log('********^^^^^', datamodel)

      this.$emit("update:clickmodel", datamodel);
      this.$emit("update:clickfrom", datafrom);
      this.$emit("update:clickindexName", indexName);
      this.$emit("update:clickindex", index);
      if (this.jsondatai) {
        this.$emit("update:jsondataindex", this.jsondatai);
      }
      this.conditionValHidden = "";
      let inview = datamodel.inview;
      if (inview) {
        let arrinview = inview.split(";");
        // sname:in:@_ref;idparent:eq:$(num)_sorder_sheng_ref;
        for (let inviewindex in arrinview) {
          let arrinviewdata = arrinview[inviewindex];
          let inviewdatas = arrinviewdata.split(":");
          if (inviewdatas[1] === "eq") {
            if (datafrom === "headdata") {
              for (let groupindex in this.jsondata.head) {
                for (let itemindex in this.jsondata.head[groupindex]
                  .groupvalue) {
                  let item =
                    this.jsondata.head[groupindex].groupvalue[itemindex];
                  if (item.sField === inviewdatas[2].split("_")[2]) {
                    this.conditionValHidden +=
                      inviewdatas[0] + ":eqref:" + item.valuehidden + ";";
                  }
                }
              }
            } else if (datafrom === "bodydata") {
              for (let itemindex in this.jsondata.bodydata[indexName][index]) {
                let item = this.jsondata.bodydata[indexName][index][itemindex];
                //  if (item.sField === inviewdatas[2].split('_')[2]) {
                if (item.sField === inviewdatas[0]) {
                  this.conditionValHidden +=
                    inviewdatas[0] + ":eqref:" + item.valuehidden + ";";
                }
              }
            }
          } else if (inviewdatas[1] === "in") {
            //console.log('-------------' + datamodel.value)
            // this.conditionValHidden += inviewdatas[0] + ':in:' + datamodel.value + ';'
            this.conditionValHidden += `${inviewdatas[0]}:${inviewdatas[1]};`;
          }
        }
      }
      let refparams = "";
      if (
        datamodel.idObject === "fee" &&
        datamodel.jsfunc === "getFeebillFyzl()"
      ) {
        //  // 费用报销单表体费用类别
        let fyzl = "";
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "fyzl") {
              fyzl = item.valuehidden;
            }
          }
        }
        if (!fyzl) {
          Toast("请先选择表头的费用种类");
          return;
        }
        let json = {
          feebill_fyzl: fyzl,
        };

        refparams = base64encode(JSON.stringify(json));
      }
      if (
        datamodel.idObject === "hdsqd" &&
        datamodel.jsfunc === "getHmnHdsqdBmToHdlx()"
      ) {
        //  // 活动类型
        let iddep = "";
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "iddep") {
              iddep = item.valuehidden;
            }
          }
        }
        if (!iddep) {
          Toast("请先选择部门");
          return;
        }
        refparams = iddep
        console.log(refparams,'refparams')

      }
      if (
        datamodel.idObject === "dlprice" &&
        datamodel.jsfunc === "chenxinHzxdlProduct()"
      ) {
        // 辰欣项目代码
        refparams = [];
        let iddep = "";
        let dlkh = "";
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "iddep") {
              iddep = item.valuehidden;
            }
            if (item.sField == "dlkh") {
              dlkh = item.valuehidden;
            }
          }
        }
        if (!dlkh) {
          Toast("请先选择联系人");
          return;
        }
        if (!iddep) {
          Toast("请先选择部门");
          return;
        }
        refparams = iddep + ";" + dlkh;
      }
      if (
        datamodel.idObject === "psxys" &&
        datamodel.jsfunc === "getProductForPsxy()"
      ) {
        // 辰欣项目代码
        refparams = [];
        let qy = "";
        let czms = "";
        let sdate = "";
        let shi = "";
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "qy") {
              qy = item.valuehidden;
              //console.log('inside...iddep' + iddep)
            }
            if (item.sField === "shi") {
              shi = item.valuehidden || "";
              //console.log('inside...iddep' + iddep)
            }
            if (item.sField == "czms") {
              czms = item.valuehidden;
              //console.log('inside...dlkh' + iddep)
            }
            if (item.sField == "sdate") {
              sdate = item.value;
              //console.log('inside...dlkh' + iddep)
            }
          }
        }
        if (!qy) {
          Toast("请先选择省份");
          return;
        }
        if (!sdate) {
          Toast("请先选择生效日期");
          return;
        }
        if (!czms) {
          Toast("请先选择操作模式");
          return;
        }
        if (shi) {
          refparams = qy + ";" + shi + ";" + sdate + ";" + czms;
        } else {
          refparams = qy + ";" + sdate + ";" + czms;
        }
      }
      if (
        datamodel.idObject === "sordersq" &&
        datamodel.jsfunc === "getSyyySordersq()"
      ) {
        // 辰欣项目代码
        refparams = [];
        let salesorg = "";
        let consumer = "";
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "salesorg") {
              salesorg = item.valuehidden;
              //console.log('inside...iddep' + iddep)
            }
            if (item.sField === "consumer") {
              consumer = item.valuehidden;
              //console.log('inside...iddep' + iddep)
            }
          }
        }
        if (!salesorg) {
          Toast("请先选择销售组织");
          return;
        }
        if (!consumer) {
          Toast("请先选择客户");
          return;
        }

        refparams = salesorg + ";" + consumer;
        
      }
      // 目标医生插入点
      if (
        datamodel.idObject === "docgraderank" &&
        datamodel.jsfunc === "gethmndocgraderankxq()"
      ) {
        refparams = [];
        let cppp = "";
        let daccount = "";
        let abg = "";
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "cppp") {
              cppp = item.valuehidden;
            }
            if (item.sField === "daccount") {
              daccount = item.valuehidden || "";
            }
            if (item.sField == "abg") {
              abg = item.valuehidden;
            }
          }
        }
        if (!daccount) {
          Toast("医院不能为空");
          return;
        }
        if (!cppp) {
          Toast("产品品牌不能为空");
          return;
        }
        if (!abg) {
          Toast("AB岗不能为空");
          return;
        }
        refparams = daccount + ";" + cppp + ";" + abg;
      }
      if (
        datamodel.idObject === "docgraderank" &&
        datamodel.jsfunc === "getmkwdocgraderankxq()"
      ) {
        refparams = [];
        let idowner = "";
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "idowner") {
              idowner = item.valuehidden;
            }
          }
        }
        if (!idowner) {
          Toast("负责人不能为空");
          return;
        }
        refparams = idowner;
      }
      // 每个逻辑需要单独写
      if (
        datamodel.idObject === "sorders" &&
        datamodel.jsfunc === "getAccountForSorder()"
      ) {
        let agent = "";
        let consumer = "";
        let type = "";
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "agent") {
              agent = item.valuehidden;
            } else if (item.sField === "consumer") {
              consumer = item.valuehidden;
            } else if (item.sField === "type") {
              type = item.valuehidden;
            }
          }
        }
        if (consumer === "") {
          Toast("请先选择客户");
          return;
        }
        if (type === "297ef43353a100de0153a1fdcaa60611") {
          if (agent !== "") {
            Toast("自营模式不能输入代理商");
            return;
          }
        } else {
          if (agent === "") {
            Toast("请先选择代理商");
            return;
          }
        }
      }

      // 迈科维，销售订单明细，海外销售订单  产品插入点 (参照PC端逻辑)
      if (
        /^sorders\d?$/.test(datamodel.idObject) &&
        datamodel.jsfunc === "getAccountForSorderMkw()"
      ) {
        let consumer = "";
        let sheng = "";
        let date = "";
        let country = "";

        const fieldMap = {
          consumer: (value) => (consumer = value),
          date: (value) => (date = value),
          sheng: (value) => (sheng = value),
          country: (value) => (country = value),
        };

        this.jsondata.head.forEach((group) => {
          group.groupvalue.forEach((item) => {
            const handler = fieldMap[item.sField];
            if (handler) {
              handler(item.valuehidden || item.value);
            }
          });
        });

        sheng = sheng.length ? sheng : "";

        if (!date) {
          Toast("单据日期不能为空");
          return;
        }

        if (consumer) {
          refparams = `${consumer};${date};${sheng};${country}`;
        } else {
          Toast("请先输入客户");
          return;
        }
      }
      // 迈科维，销售订单 经销商插入点 (参照PC端逻辑)
      if (
        /^sorder\d?$/.test(datamodel.idObject) &&
        ["getDzForSorder()", "getDzForSorder3()"].includes(datamodel.jsfunc)
      ) {
        let date = "";
        let paycompany = "";
        let tgxydh = "";
        let psxydh = "";

        const fieldMap = {
          date: (value) => (date = value),
          paycompany: (value) => (paycompany = value),
          tgxydh: (value) => (tgxydh = value),
          psxydh: (value) => (psxydh = value),
        };
        //console.log('this.jsondata', this.jsondata)
        this.jsondata.head.forEach((group) => {
          group.groupvalue.forEach((item) => {
            const handler = fieldMap[item.sField];
            if (handler) {
              handler(item.valuehidden || item.value);
            }
          });
        });

        if (!date) {
          Toast("单据日期不能为空");
          return;
        }
        if (!paycompany) {
          Toast("发货主体不能为空!");
          return;
        }
        //console.log('indexName:', indexName)
        //console.log('index:', index)
        // this.jsondata.bodydata['销售订单明细'][index].forEach(item => {
        //   const handler = fieldMap[item.sField]
        //   if (handler) {
        //     handler(item.valuehidden || item.value)
        //   }
        // });

        refparams = `${date};`;
        if (tgxydh) {
          refparams += `tgxy;${tgxydh};`;
        }
        if (psxydh) {
          refparams += `psxy;${psxydh}; `;
        }
        refparams += `${paycompany}`;
      }

      // 海默尼 活动申请单
      console.log(
        `datamodel.idobject: ${datamodel.idObject}; datamodel.jsfunc: ${datamodel.jsfunc}; fromcardid: ${this.fromcardid}`
      );
      if (
        ["fyysft", "hdfymx"].includes(datamodel.idObject) &&
        ["getHmnHdsqdSfhy()", "getHmnHdfymxSfhy()"].includes(datamodel.jsfunc)
      ) {
        refparams = this.fromcardid;
      }

      if (
        datamodel.idObject === "zjzdmx" &&
        datamodel.jsfunc === "getHmnZjzdmxCppp()"
      ) {
        this.jsondata.head.forEach((group) => {
          group.groupvalue.forEach((item) => {
            if (item.sField === "hdzjdcode") {
              refparams = item.valuehidden || item.value;
            }
          });
        });
      }

      // 海默尼 目标医生 产品品牌
      if (
        datamodel.idObject === "docgraderank" &&
        "getAccountId(this)" === datamodel.jsfunc
      ) {
        this.jsondata.head.forEach((group) => {
          group.groupvalue.forEach((item) => {
            if (item.sField === "daccount") {
              refparams = item.valuehidden || item.value;
            }
          });
        });
      }
      // 海默尼 目标终端 目标终端
      if (
        datamodel.idObject === "mbzdsqmx" &&
        datamodel.jsfunc === "getHmnMbzdsqmxBglx(this)" 
      ) {
        for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sField == 'bglx') {
                    refparams = itemdata.valuehidden
                }
              }
            }
          }
      }
      // 海默尼 目标终端 辖区编码
      if (
        datamodel.idObject === "mbzdsq" &&
        "getHmnMbzdsqIdowner()" === datamodel.jsfunc
      ) {
          for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "idowner") {
              refparams = item.valuehidden
            }
          }
        }
      }

      // 海默尼 活动申请单 活动类型
      if (
        datamodel.idObject === "hdsqd" &&
        datamodel.jsfunc === "getHmnHdsqdHdlx()"
      ) {
        this.jsondata.head.forEach((group) => {
          group.groupvalue.forEach((item) => {
            if (item.sField === "sfhy") {
              refparams = item.valuehidden;
            }
          });
        });
      }

      // 朱养心 发货单明细 批号
      if (
        datamodel.idObject === "dispatchs" &&
        datamodel.jsfunc === "getNumForDispatchs(this)"
      ) {
        const detail = this.jsondata.bodydata[indexName] || [];
        const number =
          detail[index]?.find((item) => item.sField === "quantity")?.value ||
          "";
        const batchnum = detail
          .map((item) => item.find((ite) => ite.sField === "batchnum")?.value)
          .filter((value) => value)
          .join(",");
        console.log("number:", number, "batchnum:", batchnum);
        refparams = `${number};${batchnum}`;
        console.log("refparams:", refparams);
      }

      if (
        datamodel.idObject === "accountsbgd" &&
        datamodel.jsfunc === "getOtckhbqId()"
      ) {
        let otckhbq
        let account
        let xzotckhbq
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "otckhbq") {
              console.log(otckhbq, 787878)
              // alert(item.valuehidden)
              otckhbq = item.valuehidden;
            }
            if (item.sField === "account") {
              // alert(item.valuehidden)
              account = item.valuehidden;

            }
            if (item.sField === "xzotckhbq") {
              xzotckhbq = item.valuehidden;
            }
          }
        }
        console.log(otckhbq, account, xzotckhbq, 666777)
        refparams = `${otckhbq}_${account}_${xzotckhbq}`
        // this.$store.commit("setRefInsertPointInfo", { idObject: 'accountsbgd', jsfunc: 'getOtckhbqId()', currentField: datamodel.sField, refCondition: refCondition })
      }
      if (
        datamodel.idObject === "accountsbgd" &&
        datamodel.jsfunc === "getOtckhbqIdForXz()"
      ) {
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "otckhbq") {
              refparams = item.valuehidden
            }
            if (item.sField === "bm" && item.valuehidden) {
              refparams += "_" + item.valuehidden
            }
            if (item.sField === "account" && item.valuehidden) {
              refparams += "_" + item.valuehidden
            }
            if (item.sField === "qxotckhbq" && item.valuehidden) {
              refparams += "_" + item.valuehidden
            }
          }
        }
        // this.$store.commit("setRefInsertPointInfo", { idObject: 'accountsbgd', jsfunc: 'getOtckhbqId()', currentField: datamodel.sField, refCondition: refCondition })
      }

      if (
        datamodel.idObject === "xhjhdetail" &&
        datamodel.jsfunc === "chenxinXhjhProduct()"
      ) {
        // 辰欣项目代码
        let iddep = "";
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "iddep") {
              // alert(item.valuehidden)
              iddep = item.valuehidden;
            }
          }
        }
        if (!iddep) {
          Toast("请检查部门是否为空!");
          return;
        }
        refparams = iddep;
      }

      //插入点===================================================================================

      // if (datamodel.idObject === 'accountsbgd' && datamodel.jsfunc === 'getOtckhbqId()') {
      //   console.log("%%%%", datamodel)

      //   return

      //   let refCondition = ""
      //   for (let groupindex in this.jsondata.head) {
      //     for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
      //       let item = this.jsondata.head[groupindex].groupvalue[itemindex]
      //       if (item.sField === 'account') {
      //         // alert(item.valuehidden)
      //         refparams = item.valuehidden
      //         console.log("*****", refparams)
      //       }
      //     }
      //   }
      //   // this.$store.commit("setRefInsertPointInfo", { idObject: 'accountsbgd', jsfunc: 'getOtckhbqId()', currentField: datamodel.sField, refCondition: refCondition })
      // }

      if (
        datamodel.idObject === "accountsbgd" &&
        datamodel.jsfunc === "getBmIdForLslevel()"
      ) {
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "bm" && item.sTitle === "一级部门") {
              // alert(item.valuehidden)
              if (item.valuehidden) {
                refparams = item.valuehidden;
              } else {
                Toast("请选择一级部门");
                return;
              }
            }
          }
        }
      }
      console.log(refparams)

      //end =====================================================================================

      let componentName = "ref";
      if (
        datamodel.idJoinObject === "employee" ||
        datamodel.idJoinObject === "dep"
      ) {
        componentName = "orgRef";
      }
      this.$router.push({
        name: componentName,
        params: {
          stitle: this.itemdata.sTitle,
          bo: datamodel.idJoinObject,
          tobo: datamodel.idObject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: datamodel.sRefKey,
          conditionVal: "",
          conditionValHidden: this.conditionValHidden,
          classname: datamodel.classname,
          params: refparams,
          mode: "single",
          datafrom: datafrom,
        },
      });
    },

    openRefMulti(datamodel, datafrom, indexName, index) {
      this.$emit("update:clickmodel", datamodel);
      this.$emit("update:clickfrom", datafrom);
      this.$emit("update:clickindexName", indexName);
      this.$emit("update:clickindex", index);
      this.conditionValHidden = "";
      let inview = datamodel.inview;
      if (inview !== "") {
        let arrinview = inview.split(";");
        for (let inviewindex in arrinview) {
          let arrinviewdata = arrinview[inviewindex];
          let inviewdatas = arrinviewdata.split(":");
          if (inviewdatas[1] === "eq") {
            if (datafrom === "headdata") {
              for (let groupindex in this.jsondata.head) {
                for (let itemindex in this.jsondata.head[groupindex]
                  .groupvalue) {
                  let item =
                    this.jsondata.head[groupindex].groupvalue[itemindex];
                  if (item.sField === inviewdatas[2].split("_")[2]) {
                    this.conditionValHidden +=
                      inviewdatas[0] + ":eqref:" + item.valuehidden + ";";
                  }
                }
              }
            } else if (datafrom === "bodydata") {
              for (let itemindex in this.jsondata.bodydata[indexName][index]) {
                let item = this.jsondata.bodydata[indexName][index][itemindex];
                if (item.sField === inviewdatas[0]) {
                  this.conditionValHidden +=
                    inviewdatas[0] + ":eqref:" + item.valuehidden + ";";
                }
              }
            }
          } else if (inviewdatas[1] === "in") {
            this.conditionValHidden += inviewdatas[0] + ":in:;";
          }
        }
      }
      // 每个逻辑需要单独写
      if (
        datamodel.idObject === "sorders" &&
        datamodel.jsfunc === "getAccountForSorder()"
      ) {
        let agent = "";
        let consumer = "";
        let type = "";
        for (let groupindex in this.jsondata.head) {
          for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
            let item = this.jsondata.head[groupindex].groupvalue[itemindex];
            if (item.sField === "agent") {
              agent = item.valuehidden;
            } else if (item.sField === "consumer") {
              consumer = item.valuehidden;
            } else if (item.sField === "type") {
              type = item.valuehidden;
            }
          }
        }
        if (consumer === "") {
          Toast("请先选择客户");
          return;
        }
        if (type === "297ef43353a100de0153a1fdcaa60611") {
          if (agent !== "") {
            Toast("自营模式不能输入代理商");
            return;
          }
        } else {
          if (agent === "") {
            Toast("请先选择代理商");
            return;
          }
        }
      }

      // levy 插入点============================================
      // if (datamodel.idObject === 'accountsbgd' && datamodel.jsfunc === 'getOtckhbqId()') {

      //   let refCondition = ""
      //   for (let groupindex in this.jsondata.head) {
      //     for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
      //       let item = this.jsondata.head[groupindex].groupvalue[itemindex]
      //       if (item.sField === 'otckhbq') {
      //         // alert(item.valuehidden)
      //         refCondition = item.valuehidden
      //       }
      //     }
      //   }
      //   this.$store.commit("setRefInsertPointInfo", { idObject: 'accountsbgd', jsfunc: 'getOtckhbqId()', currentField: datamodel.sField, refCondition: refCondition })
      // }

      let refparams = "";
      // if (
      //   datamodel.idObject === "accountsbgd" &&
      //   datamodel.jsfunc === "getOtckhbqId()"
      // ) {
      //   for (let groupindex in this.jsondata.head) {
      //     for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
      //       let item = this.jsondata.head[groupindex].groupvalue[itemindex];
      //       if (item.sField === "otckhbq") {
      //         // alert(item.valuehidden)
      //         refparams = item.valuehidden;
      //       }
      //     }
      //   }
      //   // this.$store.commit("setRefInsertPointInfo", { idObject: 'accountsbgd', jsfunc: 'getOtckhbqId()', currentField: datamodel.sField, refCondition: refCondition })
      // }
      // if (
      //   datamodel.idObject === "accountsbgd" &&
      //   datamodel.jsfunc === "getOtckhbqIdForXz()"
      // ) {
      //   for (let groupindex in this.jsondata.head) {
      //     for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
      //       let item = this.jsondata.head[groupindex].groupvalue[itemindex];
      //       if (item.sField === "otckhbq") {
      //         refparams = item.valuehidden;
      //       }
      //       if (item.sField === "bm") {
      //         refparams += "_" + item.valuehidden;
      //       }
      //     }
      //   }
      //   // this.$store.commit("setRefInsertPointInfo", { idObject: 'accountsbgd', jsfunc: 'getOtckhbqId()', currentField: datamodel.sField, refCondition: refCondition })
      // }
      //插入点============================================
      let refname = "refmulti";

      if (
        datamodel.idJoinObject == "employee" ||
        datamodel.idJoinObject === "dep"
      ) {
        refname = "orgRef";
      }
      this.$router.push({
        name: refname,
        params: {
          stitle: this.itemdata.sTitle,
          bo: datamodel.idJoinObject,
          tobo: datamodel.idObject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: datamodel.sRefKey,
          conditionVal: "",
          conditionValHidden: this.conditionValHidden,
          classname: datamodel.classname,
          valuehidden1: datamodel.valuehidden,
          datamodel1: datamodel,
          refparams: refparams,
          mode: "multiple",
        },
      });
    },
  },
};
</script>
